/**
 * @file logger.h
 * @brief High-performance logging system for H743-V2 Flight Control System
 * @author Flight Control Team
 * @date 2025-10-31
 */

#ifndef __LOGGER_H
#define __LOGGER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>

/**
 * @brief Log level
 */
typedef enum {
    LOG_LEVEL_NONE = 0,
    LOG_LEVEL_ERROR,
    LOG_LEVEL_WARN,
    LOG_LEVEL_INFO,
    LOG_LEVEL_DEBUG,
    LOG_LEVEL_TRACE
} LogLevel_t;

/**
 * @brief Log output destination
 */
typedef enum {
    LOG_OUTPUT_NONE     = 0x00,
    LOG_OUTPUT_USB      = 0x01,
    LOG_OUTPUT_UART     = 0x02,
    LOG_OUTPUT_SDCARD   = 0x04,
    LOG_OUTPUT_ALL      = 0xFF
} LogOutput_t;

/**
 * @brief Log message structure
 */
typedef struct {
    uint32_t timestamp;     // Timestamp in milliseconds
    LogLevel_t level;       // Log level
    const char *tag;        // Log tag (module name)
    char message[256];      // Log message
} LogMessage_t;

/**
 * @brief Binary log entry structure (for high-speed logging)
 */
typedef struct __attribute__((packed)) {
    uint32_t timestamp;     // Timestamp in microseconds
    uint16_t msg_id;        // Message ID
    uint16_t length;        // Data length
    uint8_t data[256];      // Binary data
} BinaryLogEntry_t;

/**
 * @brief Logger configuration
 */
typedef struct {
    LogLevel_t level;       // Minimum log level
    uint8_t output;         // Output destinations (bitmask)
    bool enable_colors;     // Enable ANSI colors for USB/UART
    bool enable_timestamp;  // Enable timestamp in messages
    bool enable_binary;     // Enable binary logging
    uint32_t buffer_size;   // Log buffer size
} LoggerConfig_t;

/**
 * @brief Initialize logger
 * @return 0 on success, -1 on error
 */
int Logger_Init(void);

/**
 * @brief Deinitialize logger
 */
void Logger_DeInit(void);

/**
 * @brief Configure logger
 * @param config Logger configuration
 * @return 0 on success, -1 on error
 */
int Logger_Configure(const LoggerConfig_t *config);

/**
 * @brief Set log level
 * @param level Minimum log level
 */
void Logger_SetLevel(LogLevel_t level);

/**
 * @brief Set log output destinations
 * @param output Output destinations (bitmask)
 */
void Logger_SetOutput(uint8_t output);

/**
 * @brief Log message with level
 * @param level Log level
 * @param tag Log tag
 * @param format Format string
 * @param ... Variable arguments
 */
void Logger_Log(LogLevel_t level, const char *tag, const char *format, ...);

/**
 * @brief Log binary data
 * @param msg_id Message ID
 * @param data Pointer to data
 * @param length Data length
 * @return 0 on success, -1 on error
 */
int Logger_LogBinary(uint16_t msg_id, const void *data, uint16_t length);

/**
 * @brief Flush log buffer to SD card
 * @return 0 on success, -1 on error
 */
int Logger_Flush(void);

/**
 * @brief Get number of log entries in buffer
 * @return Number of entries
 */
uint32_t Logger_GetBufferCount(void);

/**
 * @brief Get logger statistics
 * @param total_logs Total number of logs
 * @param dropped_logs Number of dropped logs
 * @param buffer_usage Buffer usage percentage
 */
void Logger_GetStats(uint32_t *total_logs, uint32_t *dropped_logs, uint8_t *buffer_usage);

/**
 * @brief Create new log file on SD card
 * @param filename Log file name (NULL for auto-generated)
 * @return 0 on success, -1 on error
 */
int Logger_CreateFile(const char *filename);

/**
 * @brief Close current log file
 */
void Logger_CloseFile(void);

/**
 * @brief Download log file via USB
 * @param filename Log file name
 * @return 0 on success, -1 on error
 */
int Logger_DownloadFile(const char *filename);

/* Convenience macros */
#define LOG_ERROR(tag, fmt, ...)   Logger_Log(LOG_LEVEL_ERROR, tag, fmt, ##__VA_ARGS__)
#define LOG_WARN(tag, fmt, ...)    Logger_Log(LOG_LEVEL_WARN, tag, fmt, ##__VA_ARGS__)
#define LOG_INFO(tag, fmt, ...)    Logger_Log(LOG_LEVEL_INFO, tag, fmt, ##__VA_ARGS__)
#define LOG_DEBUG(tag, fmt, ...)   Logger_Log(LOG_LEVEL_DEBUG, tag, fmt, ##__VA_ARGS__)
#define LOG_TRACE(tag, fmt, ...)   Logger_Log(LOG_LEVEL_TRACE, tag, fmt, ##__VA_ARGS__)

/* Binary log message IDs */
#define LOG_MSG_IMU_DATA            0x0001
#define LOG_MSG_MAG_DATA            0x0002
#define LOG_MSG_BARO_DATA           0x0003
#define LOG_MSG_GPS_DATA            0x0004
#define LOG_MSG_ATTITUDE            0x0010
#define LOG_MSG_POSITION            0x0011
#define LOG_MSG_VELOCITY            0x0012
#define LOG_MSG_CONTROL_OUTPUT      0x0020
#define LOG_MSG_MOTOR_OUTPUT        0x0021
#define LOG_MSG_RC_INPUT            0x0030
#define LOG_MSG_SYSTEM_STATUS       0x0040

#ifdef __cplusplus
}
#endif

#endif /* __LOGGER_H */

